package com.tencreat.account.controller.admin;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tencreat.common.annotation.Log;
import com.tencreat.common.annotation.RepeatSubmit;
import com.tencreat.common.core.HttpResult;
import com.tencreat.common.core.controller.BaseController;
import com.tencreat.common.core.domain.AjaxResult;
import com.tencreat.common.core.domain.model.ReqIdParam;
import com.tencreat.common.core.domain.model.ReqIdsParam;
import com.tencreat.common.enums.BusinessType;
import com.tencreat.common.utils.StringUtils;
import com.tencreat.common.utils.poi.ExcelUtil;
import com.tencreat.domain.account.MallStore;
import com.tencreat.domain.account.MallSupplier;
import com.tencreat.domain.account.dto.MallStoreDTO;
import com.tencreat.domain.account.dto.MallSupplierDTO;
import com.tencreat.domain.account.dto.MallSupplierRequDTO;
import com.tencreat.domain.account.vo.MallStoreRespVo;
import com.tencreat.domain.account.vo.MallStoreStaticsVo;
import com.tencreat.domain.account.vo.MallSupplierRespVo;
import com.tencreat.domain.account.vo.MallSupplierStaticsVo;
import com.tencreat.domain.mall.dto.MallStoreAuditDTO;
import com.tencreat.domain.mall.dto.MallSupplierAuditDTO;
import com.tencreat.service.account.IMallRegionService;
import com.tencreat.service.account.IMallStoreService;
import com.tencreat.service.account.IMallSupplierService;
import com.tencreat.service.account.IMallUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 店铺Controller
 *
 * @author tencreat
 * @date 2021-08-15
 */
@Api(tags = "店铺api")
@RestController
@RequestMapping("/mall/store")
public class AdminMallStoreController extends BaseController {
    @Autowired
    private IMallStoreService mallStoreService;
    @Autowired
    private IMallUserService userService;
    @Autowired
    private IMallRegionService mallRegionService;

    /**
     * 查询店铺列表
     */
    @ApiOperation(value = "查询店铺列表")
    @PreAuthorize("@ss.hasPermi('mall:mallStore:list')")
    @PostMapping("/list")
    public HttpResult<IPage<MallStoreRespVo>> list(@RequestBody MallStore mallStore) {
        Page<MallStore> page = new Page<>(mallStore.getPageNum(), mallStore.getPageSize());
        return HttpResult.ok(mallStoreService.queryAll(page,mallStore));
    }

    /**
     * 新增店铺
     */
    @ApiOperation(value = "新增店铺")
    @PreAuthorize("@ss.hasPermi('mall:mallStore:add')")
    @Log(title = "用户管理>店铺列表", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    @RepeatSubmit
    public HttpResult<String> add(@RequestBody MallStoreDTO mallStoreVO) {
        return mallStoreService.insertMallStore(mallStoreVO);
    }



    /* *
     * 导出店铺列表
     */
    @ApiOperation(value = "导出店铺列表")
    @PreAuthorize("@ss.hasPermi('mall:mallStore:export')")
    @Log(title = "用户管理>店铺列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public AjaxResult export(@RequestBody MallStore mallStore) {
        Page<MallStore> page = new Page<>(1, Integer.MAX_VALUE);
        IPage<MallStoreRespVo> mallStorePage = mallStoreService.queryAll(page, mallStore);


//        List<MallSupplierRespVo> list = supplierPage.getRecords();
        mallStorePage.convert(item->{
            MallStore mallStore1 = new MallStore();
            BeanUtils.copyProperties(item, mallStore1);
            mallStore1.setAreasName(item.getProvinceName()+item.getCityName()+item.getCountyName());
            mallStore1.setApplyTime(item.getCreateTime());

            return mallStore1;
        });
        List<MallStore> list = JSONObject.parseArray(JSONObject.toJSONString(mallStorePage.getRecords()), MallStore.class);
        ExcelUtil<MallStore> util = new ExcelUtil<MallStore>(MallStore.class);
        return util.exportExcel(list, "partner");
    }

    /**
     * 获取店铺详细信息
     */
    @ApiOperation(value = "获取店铺详细信息")
    @PreAuthorize("@ss.hasPermi('mall:MallStore:query')")
    @PostMapping(value = "/getInfo")
    public HttpResult<MallStore> getInfo(@RequestBody ReqIdParam id) {
        return HttpResult.ok(mallStoreService.selectMallStoreById(id));
    }

    @ApiOperation(value = "重置密码")
    @PostMapping("/forget_password")
    public HttpResult<Boolean> forgetPassword( @RequestBody MallSupplierRequDTO mallStore) {
        return mallStoreService.password(mallStore);
    }

    /**
     * 修改店铺
     */
    @ApiOperation(value = "修改店铺")
    @PreAuthorize("@ss.hasPermi('mall:mallStore:edit')")
    @Log(title = "用户管理>供应商列表", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/edit")
    public HttpResult<String> edit(@RequestBody MallStore mallStore) {
        return mallStoreService.updateMallStore(mallStore);
    }

    @ApiOperation(value = "店铺审核")
    @PreAuthorize("@ss.hasPermi('mall:mallStore:audit')")
    @Log(title = "用户管理>店铺列表", businessType = BusinessType.AUDIT)
    @PostMapping(value = "/audit")
    @RepeatSubmit
    public HttpResult<String> audit(@RequestBody @Validated MallStoreAuditDTO dto) {
        mallStoreService.audit(dto);
        return HttpResult.ok();
    }

    /**
     * 删除店铺
     */
    @ApiOperation(value = "删除店铺")
    @PreAuthorize("@ss.hasPermi('mall:mallStore:remove')")
    @Log(title = "用户管理>供应商列表", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    public HttpResult<Boolean> remove(@RequestBody ReqIdsParam ids) {
        return to(mallStoreService.deleteMallStoreByIds(ids));
    }

    /**
     * 根据用户编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('mall:mallStore:query')")
    @GetMapping(value = { "/", "/{userId}" })
    public AjaxResult getInfo(@PathVariable(value = "userId", required = false) Long userId)
    {
        AjaxResult ajax = AjaxResult.success();
        ajax.put("users", userService.selectpartner());
        if (StringUtils.isNotNull(userId))
        {
            ajax.put(AjaxResult.DATA_TAG, mallStoreService.selectMallStoreById(userId));
        }
        return ajax;
    }

    /**
     * 店铺统计
     */
    @ApiOperation(value = "店铺统计")
    @PreAuthorize("@ss.hasPermi('mall:mallStore:list')")
    @GetMapping("/mallStoreStatics")
    public HttpResult<MallStoreStaticsVo> mallStoreStatics() {
        return HttpResult.ok(mallStoreService.storeStatics());
    }
}
